package com.ccl.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Component
public class EmailUtils {
    //邮件API:
    @Autowired
    private   JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private   String from;

    //发送邮件 :邮件发送给谁,邮件主题,邮件内容
    public  void sendEmail(String to, String subject, String content) throws MessagingException {
        //创建邮件对象
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //使用工具类对邮件对象初始化
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(from);//邮件发送者
        helper.setTo(to);//发送给谁
        helper.setSubject(subject);//邮件主题
        helper.setText(content);//邮件内容
        mailSender.send(mimeMessage);
    }
}
